#pragma once

namespace gm
{
	template<class T>
	class vector
	{
	public:
		typedef T* iterator;
		typedef const T* const_iterator;

		vector()
		{}

		~vector()
		{
			if (_start)
			{
				delete[] _start;
				_start = _finish = _endofstorage = nullptr;
			}
		}

		iterator begin()
		{
			return _start;
		}

		iterator end()
		{
			return _finish;
		}


		const_iterator begin() const
		{
			return _start;
		}

		const_iterator end() const
		{
			return _finish;
		}


		void reserve(size_t  n)
		{
			if (n > capacity())
			{
				size_t old = size();
				T* tmp = new T[n];

				if (_start)
				{
					memcpy(tmp, _start, old * sizeof(T)) :
						delete[] _start;
				}

				_start = tmp;
				_finsih = _start + old;
				_endofstorage = _start + n;

			}
		}

		void push_back(const T& x)
		{
			if (_finish == endofstorage)
			{
				size_t newcapacity = capacity() == 0 ? 4 : capacity() * 2;
				reserve(newcapacity);
			}

			*_finish = x;
			_finish++;
		}

		size_t size() const
		{
			return _finish - _start;
		}

		size_t capacity() const
		{
			return _endofstorage - _start;
		}

	private:
		iterator _start = nullptr;
		iterator _finish = nullptr;
		iterator _endofstorage = nullptr;
	};

	void test_vector1()
	{
		vector<int> v;
		v.push_back(1);
		v.push_back(1);
		v.push_back(1);
		v.push_back(1);

		vector<int>::iterator vit = v.begin();

		while (vit != v.end())
		{
			cout << *vit << " ";
			vit++;
		}
		cout << endl;
	}

}